import pygame, math, sys, random
from dinosaurs import Dinosaur
from health import Healthbar
#stuff 
class Terminator(pygame.sprite.Sprite):
    def __init__(self, position, screenSize):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.images = []
        self.images += [pygame.image.load("rsc/Player/player.png")]
        self.frame = 0
        self.image = self.images[self.frame]
        self.rect = self.image.get_rect()
        self.place(position)
        self.radius = self.rect.width/2
                                     #2.5
        self.maxSpeed = 5
        self.speed= [0,0]
        self.realx = position[0]
        self.realy = position[1]
        self.x = position[0]
        self.y = position[1]
        self.realx = 0
        self.realy = 0
        self.scrollThreshold = 100#250
        self.scrollingx = False
        self.scrolling = False
        self.scrollingy = False
        self.damage = 5
        self.health = 75
        self.maxHealth = 75
        self.living = True
        self.didHit = False
        self.dir = "right"
        self.screenSize = self.screenWidth, self.screenHeight = screenSize
    
    def place(self, position):
        self.rect.center = position
        #print "I've moved to", position
    
    def direction(self, dir):
        if dir == "left":    
            self.dir = "left"
            self.speed = [-self.maxSpeed, 0]
        elif dir == "right":
            self.dir = "right"
            self.speed = [self.maxSpeed, 0]
        elif dir == "up":
            self.dir = "up"
            self.speed = [0, -self.maxSpeed]
        elif dir == "down":
            self.dir = "down"
            self.speed = [0, self.maxSpeed]
        elif dir == "stop":
            self.speed = [0,0]
        elif dir == "go":
            self.speed = [1,1]
    
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
    
    def move(self):
        self.rect = self.rect.move(self.speed)
     
        self.realx += self.speed[0]
        self.x += self.speed[0]
        if self.x >= self.screenWidth - self.scrollThreshold:
            self.x = self.screenWidth - self.scrollThreshold
            self.scrollingx = True
        elif self.x <= self.scrollThreshold:
            self.x = self.scrollThreshold
            self.scrollingx = True
        else:
            self.scrollingx = False
            
        self.realy += self.speed[1]
        self.y += self.speed[1]
        print self.y
        if self.y >= self.screenHeight - self.scrollThreshold:
            self.y = self.screenHeight - self.scrollThreshold
            self.scrollingy = True
        elif self.y <= self.scrollThreshold:
            self.y = self.scrollThreshold
            self.scrollingy = True
        else:
            self.scrollingy = False
        
        self.place([self.x, self.y])
        
    def hurt(self, amount):
        self.health -= amount
        if self.health <= 0:
           self.kill()
        
    def dinosaurCollide(self, other):
        if (self.distToPoint(other.rect.center) 
            < self.radius + other.radius):
                other.speed[0] = other.speed[0] * -1
                other.speed[1] = other.speed[1] * -1
                self.didHit = True
                other.didHit = True
                self.health -= 3
                print self.health
                
				
    def wallCollide(self):
        if (self.rect.left < 0 
            or self.rect.right > self.screenWidth):
            self.speed[0] = 0
        if (self.rect.top < 0 
            or self.rect.bottom >self.screenHeight):
			self.speed[1] = 0
            
    def collide_point(self, pt, rad):
        if self.distToPoint(pt) <= rad + self.radius:
            return True
        else:
            return False   
			         
    def update(*args):
        self = args[0]
        self.xOffset = args[1]
        self.didHit = False
        if self.health < 0:
            self.kill()
        self.move()
        self.wallCollide()
        #self.dinosaurCollide()
         
    